package util;

/**
 * 算法工具类
 *
 * @author yujiafan
 * @version 1.0.0
 * @create 2023-03-03
 */
public class MathUtil {

    /**
     * 交换数组位置
     */
    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    /**
     * 随机生成数组
     */
    public static int[] generateArray(int maxValue, int maxSize) {
        int size = (int) (Math.random() * (maxSize + 1));
        int[] arr = new int[size];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * (maxValue + 1)) - (int) (Math.random() * maxValue);
        }
        return arr;
    }

    /**
     * 复制数组
     */
    public static int[] copyArray(int[] arr) {
        if (arr == null || arr.length == 0) return new int[0];
        int[] copy = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            copy[i] = arr[i];
        }
        return copy;
    }

    /**
     * 两个数组是否相同
     */
    public static boolean isEqualArray(int[] arr1, int[] arr2) {
        if (arr1 == null && arr2 != null) return false;
        if (arr1 != null && arr2 == null) return false;
        if (arr1 == null && arr2 == null) return true;
        if (arr1.length != arr2.length) return false;
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) return false;
        }
        return true;
    }

}
